using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace Client.Converters
{
    [ValueConversion(typeof(decimal), typeof(String))]
    public class DecimalConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var val = (decimal)value;
            return val.ToString(culture);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var strValue = value as string;
            strValue = string.IsNullOrEmpty(strValue) ? "0" : strValue;
            decimal val;
            return decimal.TryParse(strValue, out val) ? val : DependencyProperty.UnsetValue;
        }
    }
}